const path = require('path')

module.exports = {
  transpileDependencies: ['@dcloudio/uni-ui'],
  configureWebpack: {
    optimization: {
      // 启用JS压缩
      minimize: true,
      // 启用tree-shaking
      usedExports: true,
      // 代码分割配置
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          vendors: {
            name: 'chunk-vendors',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          commons: {
            name: 'chunk-commons',
            minChunks: 2,
            priority: 5,
            chunks: 'initial',
            reuseExistingChunk: true
          }
        }
      }
    }
  },
  chainWebpack: config => {
    // 添加组件按需注入的处理
    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => {
        return {
          ...options,
          compilerOptions: {
            // 启用按需加载组件
            isCustomElement: tag => tag.startsWith('uni-')
          }
        }
      })
    
    // 生产环境配置
    if (process.env.NODE_ENV === 'production') {
      // 开启JS文件压缩
      config.optimization.minimize(true)
      // 开启CSS压缩
      config.optimization.minimizer('terser').tap(args => {
        const compress = args[0].terserOptions.compress
        // 清除console和debugger
        compress.drop_console = true
        compress.drop_debugger = true
        return args
      })
    }
  }
} 